package com.qyhl.webtv.module_news.news.normal;

import android.util.Log;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.launcher.ARouter;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.ServicePathConstant;
import com.qyhl.webtv.commonlib.entity.intergral.CoinBean;
import com.qyhl.webtv.commonlib.entity.news.AdvHomeBean;
import com.qyhl.webtv.commonlib.entity.news.NormalNewsBean;
import com.qyhl.webtv.commonlib.service.IntergralService;
import com.qyhl.webtv.module_news.common.NewsUrl;

import java.util.List;

/**
 * Created by qyhl on 2017/11/17.
 */

public class NormalNewsModel implements NormalNewsContract.NormalModel {
    private NormalNewsPresenter mPresenter;

    public NormalNewsModel(NormalNewsPresenter mPresenter) {
        this.mPresenter = mPresenter;
        ARouter.getInstance().inject(this);
    }


    @Autowired(name = ServicePathConstant.INTERGRAL_SERICE)
    IntergralService intergralService;

    /**
     * 获取新闻详情
     *
     * @param newsId
     */
    @Override
    public void getDetail(String newsId) {
        String userName = CommonUtils.getInstance().getUserName();

        EasyHttp.get(NewsUrl.GET_GENERAL_NEWS_DETAIL)
                .params("tagName", CommonUtils.getInstance().getSiteName())
                .params("userName", userName)
                .params("siteId", CommonUtils.getInstance().getSite()+ "")
                .params("newsId", newsId)
                .execute(new SimpleCallBack<NormalNewsBean>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.detailError(1, "暂无任何数据！");
                    }

                    @Override
                    public void onSuccess(NormalNewsBean normalNewsBean) {
                        mPresenter.setDetail(normalNewsBean);
                    }
                });

    }

    /**
     * 获取评论列表
     *
     * @param newsId
     */
    @Override
    public void getComment(String newsId) {

        EasyHttp.get(NewsUrl.GET_GENERAL_NEWS_DETAIL)
                .params("tagName", CommonUtils.getInstance().getSiteName())
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("newsId", newsId)
                .execute(new SimpleCallBack<NormalNewsBean>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.commentError(1, "暂无任何评论！");
                    }

                    @Override
                    public void onSuccess(NormalNewsBean normalNewsBean) {
                        mPresenter.commentSuccess(normalNewsBean.getArticleComment());
                    }
                });

    }

    /**
     * 获取新闻内广告
     *
     * @param catalogId
     */
    @Override
    public void getAdv(String catalogId) {

        EasyHttp.get(NewsUrl.GET_DETAIL_ADV)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("catalogId", catalogId)
                .execute(new SimpleCallBack<List<AdvHomeBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.advError();
                    }

                    @Override
                    public void onSuccess(List<AdvHomeBean> advHomeBeans) {
                        mPresenter.setAdv(advHomeBeans);
                    }
                });

    }

    /**
     * 点击量统计
     *
     * @param newsId
     */
    @Override
    public void count(String newsId) {


        EasyHttp.get(NewsUrl.GET_NEWS_DETAIL_COUNT)
                .params("newsId", newsId)
                .params("tagName", CommonUtils.getInstance().getSiteName())
                .params("siteId", String.valueOf(CommonUtils.getInstance().getSite()))
                .execute(new SimpleCallBack<String>() {

                    @Override
                    public void onError(ApiException e) {

                    }

                    @Override
                    public void onSuccess(String s) {
                        if ( s== null) {
                        } else {
                            Log.i("success_count", "点击数统计成功！");
                        }
                    }
                });

    }

    /**
     * 收藏
     *
     * @param newsId
     */
    @Override
    public void collect(String newsId) {
        String url = CommonUtils.getInstance().getCmsUrl();
        String userName = CommonUtils.getInstance().getUserName();

        EasyHttp.get(url)
                .params("tagName", CommonUtils.getInstance().getSiteName())
                .params("method", "collectArticle")
                .params("userName", userName)
                .params("siteId", String.valueOf(CommonUtils.getInstance().getSite()))
                .params("articleID", newsId)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.collectError("网络异常，请稍后重试！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        if (s.contains("SUCCESS")) {
                            mPresenter.collectSuccess("成功收藏");
                        } else {
                            mPresenter.collectError("收藏失败，请稍后重试！");
                        }
                    }
                });

    }

    /**
     * 取消收藏
     *
     * @param newsId
     */
    @Override
    public void cancelCollect(String newsId) {
        String url = CommonUtils.getInstance().getCmsUrl();
        String userName = CommonUtils.getInstance().getUserName();

        EasyHttp.get(url)
                .params("tagName", CommonUtils.getInstance().getSiteName())
                .params("method", "deleteCollectArticle")
                .params("userName", userName)
                .params("siteId", String.valueOf(CommonUtils.getInstance().getSite()))
                .params("articleID", newsId)
                .execute(new SimpleCallBack<String>() {

                    @Override
                    public void onError(ApiException e) {
                        mPresenter.cancelCollectError("网络异常，请稍后重试!");
                    }

                    @Override
                    public void onSuccess(String s) {
                        if (s.contains("SUCCESS")) {
                            mPresenter.cancelCollectSuccess();
                        } else {
                            mPresenter.cancelCollectError("取消失败，请稍后重试！");
                        }
                    }
                });
    }

    /**
     * 点赞
     *
     * @param newsId
     */
    @Override
    public void like(String newsId) {
        String url = CommonUtils.getInstance().getCmsUrl();

        EasyHttp.get(url)
                .params("tagName", CommonUtils.getInstance().getSiteName())
                .params("method", "getGoodViewCount")
                .params("ID", newsId)
                .execute(new SimpleCallBack<String>() {

                    @Override
                    public void onError(ApiException e) {
                        mPresenter.likeError("出错了,请稍后重试！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        if (s.contains("0")) {
                            mPresenter.likeSuccess();
                        } else {
                            mPresenter.likeError("出错了,请稍后重试！");
                        }
                    }
                });


    }

    /**
     * 取消点赞
     *
     * @param newsId
     */
    @Override
    public void cancelLike(String newsId) {
        String url = CommonUtils.getInstance().getCmsUrl();
        EasyHttp.get(url)
                .params("tagName", CommonUtils.getInstance().getSiteName())
                .params("method", "getCancelGoodViewCount")
                .params("ID", newsId)
                .execute(new SimpleCallBack<String>() {

                    @Override
                    public void onError(ApiException e) {
                        mPresenter.cancelLikeError("出错了,请稍后重试！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        if (s.contains("0")) {
                            mPresenter.cancelLikeSuccess();
                        } else {
                            mPresenter.cancelLikeError("出错了,请稍后重试！");
                        }
                    }
                });
    }

    /**
     * 发送评论
     *
     * @param title
     * @param catalogId
     * @param type
     * @param id
     * @param commentUser
     * @param content
     */
    @Override
    public void sendComment(String title, String catalogId, String type, String id, String commentUser, String content) {
        String url = CommonUtils.getInstance().getCmsUrl();
        String headicon = CommonUtils.getInstance().getUserLogo();

        EasyHttp.post(url)
                .params("tagName", CommonUtils.getInstance().getSiteName())
                .params("method", "addComment")
                .params("siteId", String.valueOf(CommonUtils.getInstance().getSite()))
                .params("Title", content)
                .params("catalogId", catalogId)
                .params("catalogType", type)
                .params("articleId", id)
                .params("commentUser", commentUser)
                .params("content", content)
                .params("ip", "127.0.0.1")
                .params("logo", headicon)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.sendError("网络异常，跟帖失败，请稍后重试!");
                    }

                    @Override
                    public void onSuccess(String s) {
                        if (s.contains("SUCCESS")) {
                            mPresenter.sendSuccess("跟帖成功，等待审核!");
                        } else {
                            mPresenter.sendError("跟帖失败，请稍后重试!");
                        }
                    }
                });

    }

    @Override
    public void getCoin(String username) {

        intergralService.gainCoin("", "readNews", new IntergralService.GainCoinCallBack() {
            @Override
            public void success(CoinBean coinBean) {
                mPresenter.getCoinSuccess(coinBean);
            }

            @Override
            public void error(String msg) {
                mPresenter.getCoinFailure(msg);
            }
        });

    }

    @Override
    public void getCommentCoin(String name, String type) {
        intergralService.gainCoin(type, name, new IntergralService.GainCoinCallBack() {
            @Override
            public void success(CoinBean coinBean) {
                mPresenter.getCommentCoinSuccess(coinBean);
            }

            @Override
            public void error(String msg) {
                mPresenter.getCommentCoinFailure(msg);
            }
        });
    }

    @Override
    public void shareCount(String newsId) {
        EasyHttp.get(NewsUrl.GET_SHARE_COUNT)
                .params("tagName", CommonUtils.getInstance().getSiteName())
                .params("siteId", CommonUtils.getInstance().getSite()+"")
                .params("newsId", newsId)
                .execute(new SimpleCallBack<String>() {

                    @Override
                    public void onError(ApiException e) {
                        Log.e("@@@","统计失败！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        Log.i("@@@","统计成功！");
                    }
                });
    }

}
